/*
 * Project Name spring-boot-tools
 * File Name BaseController
 * Package Name com.huxiaosu.tools.mybatis.base.controller
 * Create Time 2024/7/15
 * Create by name：liujie
 */
package com.huxiaosu.tools.mybatis.base.controller;

import com.huxiaosu.tools.common.annotation.ToolsController;
import com.huxiaosu.tools.common.base.R;
import com.huxiaosu.tools.common.validator.Add;
import com.huxiaosu.tools.common.validator.Update;
import com.huxiaosu.tools.common.validator.ValidatorUtils;
import com.huxiaosu.tools.mybatis.base.BaseDto;
import com.huxiaosu.tools.mybatis.base.PageInfo;
import com.huxiaosu.tools.mybatis.base.service.BaseService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Description
 *
 * @author liujie
 * @date 2024/7/15 17:57
 */
@Slf4j
@ToolsController
public abstract class BaseController<Service extends BaseService<Dto, Entity>, Dto extends BaseDto, Entity> {
    @Autowired
    protected Service baseService;
    @PutMapping
    public R<?> update(@RequestBody Dto dto) {
        // 效验数据
        ValidatorUtils.validateEntity(dto, Update.class, Add.class);
        Entity entity = baseService.getById(dto.getId());
        if (entity == null) {
            return R.failed(-1,"数据不存在");
        }
        BeanUtils.copyProperties(dto, entity);
        baseService.update(entity);
        return R.success();
    }

    /**
     * ID非自增请重写当前接口
     */
    @SneakyThrows
    @PostMapping
    public R<?> save(@RequestBody Dto dto) {
        // 效验数据
        ValidatorUtils.validateEntity(dto, Add.class);
        Entity entity = getTClass().getDeclaredConstructor().newInstance();
        BeanUtils.copyProperties(dto, entity);

        baseService.save(entity);
        return R.success();
    }
    @GetMapping("/info/{id}")
    public R<Entity> getList(@PathVariable("id") String id) {
        // 效验数据
        Entity byId = baseService.getById(id);
        return R.success(byId);
    }

    @DeleteMapping("deleteById/{id}")
    public R<?> delete(@PathVariable("id") String id) {
        baseService.deleteById(id);
        return R.success();

    }

    /**
     * 删除
     */
    @PostMapping("/deleteByIds")
    public R<?> deleteByIds(@RequestBody List<Serializable> ids) {
        baseService.deleteByIds(ids);
        return R.success();
    }

    // @NoRepeatSubmit
    @PostMapping("/getList")
    public R<PageInfo<Entity>> getList(@RequestBody Dto dto) {
        // 效验数据
        PageInfo<Entity> list = baseService.getList(dto);
        return R.success(list);
    }
    @SuppressWarnings("unchecked")
    protected Class<Entity> getTClass() {
        return (Class<Entity>) ((ParameterizedType) this.getClass().getGenericSuperclass())
                .getActualTypeArguments()[2];
    }

}